package com.ma.controller;

import com.ma.Utils.filesUtil;
import com.ma.entity.Files;
import com.ma.entity.Message;
import com.ma.entity.Student;
import com.ma.entity.Team;
import com.ma.server.FilesServer;
import com.ma.server.MessageServe;
import com.ma.server.StudentServer;
import com.ma.server.teamServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ProjectName: project
 * @Package: com.ma.controller
 * @ClassName: MessageCon
 * @Author: YHHHH
 * @Description:
 * @Date: 2021/3/20 15:03
 * @Version: 1.0
 */
@RestController
@CrossOrigin
public class MessageCon {

    @Autowired
    private MessageServe messageServe;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private teamServer teamServer;

    @Autowired
    private StudentServer studentServer;

    @Autowired
    private FilesServer filesServer;

    @Autowired
    private filesUtil filesUtil;

    @GetMapping("/message/findById")
    public List<Message> findById(@RequestParam("receiveID") String receiveID) {
            List<Message> findById = messageServe.findById(receiveID);
                return findById;
    }

    @GetMapping("/message/findReadedMessage")
    public List<Message> findReadedMessage(@RequestParam("receiveID") String receiveID) {
        List<Message> findById = messageServe.findReadedMessage(receiveID);
        return findById;
    }

    /*
    发送退组的消息
     */
    @GetMapping("/message/sendExitMessage")
    public String sendExitMessage(@RequestParam("number") String sendID) {
        redisTemplate.setStringSerializer(new StringRedisSerializer());
        Student student = (Student) redisTemplate.opsForValue().get(sendID);
        String studentName = student.getStudentName();                  //拿到姓名
        Integer teamID = studentServer.findTeamID(sendID);              //找到组号
        String leader = teamServer.findLeader(teamID);                  //找到组长的number
        System.out.println("组长："+leader+"申请的："+sendID);
        System.out.println(leader.equals(sendID));
        List<String> allTeammateNum = studentServer.findAllTeammateNum(teamID);//找到组员
        // 为1说明小组只剩下自己  可以直接退队
        if (allTeammateNum.size()==1){
            String s = allTeammateNum.get(0);
            Integer integer = studentServer.updateStudentTeam(s,null,0);   //修改学生的组号
            redisTemplate.delete(s);
            List<Files> byNumber = filesServer.findByNumber(s);
            for (Files files : byNumber) {
                Integer filesId = files.getFilesId();
                filesUtil.filesDelete(filesId);
            }
            Integer integer2 = filesServer.deleteStudentFiles(s);      //将文件从数据库删除
            return "success";
        }
        if (leader.equals(sendID)) {
            return "您是该小组的组长，请先转让队长！";
        } else {
            Message message = new Message();
            message.setMessage("【退队审核】收到来自" + studentName + "学号（" + sendID + ")的退组申请，请及时审核!");
            message.setSendId(sendID);
            message.setReceiveId(leader);
            Integer integer = messageServe.addMessage(message);
            return "success";
        }
    }
    /*
    改变消息的状态为已读
     */
    @GetMapping("/message/haveRead")
    public String haveRead(@RequestParam("id")Integer id){
        Integer state = messageServe.updateState(id);
        return "success";
    }

}
